home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 35 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.7 KB  |  94 lines

  1. Path: in1.uu.net!bounce-back
  2. Date: 12 Jan 96 17:20:17 GMT
  3. Approved: fjh@cs.mu.oz.au
  4. Organization: -
  5. Newsgroups: comp.std.c++
  6. Return-Path: <daemon@meeker.UCAR.EDU>
  7. Message-ID: <m0talmh-000FHlC@redline.ru>
  8. From: mike <mike@redline.ru>
  9. X-Original-Date: Fri, 12 Jan 96 19:50:39 -800
  10. X-Mailer: Mozilla/0.94 Beta (Windows)
  11. Subject: Overloading 'operator new': errors in MSVC 2.0 or not?
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMPaYa+EDnX0m9pzZAQF3fwF/fILkqVnfHBrCNkZ95pm5Lphl7ztJUYHk
  14.     1IcFREZ+WYWu1X5fR5i7bC0QgLIRZBRN
  15.     =xb2S
  16.  
  17. I think I've discovered some errors in Microsoft Visual C++ 2.0 compiler.
  18.  
  19. 1) Suppose we want to overload global 'operator new':
  20.  
  21. void* operator new (size_t size, MemoryAllocator* palloc)
  22. {
  23.     // Allocate memory using special allocator
  24.     return palloc->alloc (size);
  25. }
  26.  
  27. How to delete object allocated by this function?
  28. We can't overload 'operator delete' (according to ARM 5.3.4, 12.5).
  29. We must write something like this:
  30.  
  31. void Delete_SomeClass (SomeClass* p, MemoryAllocator* palloc)
  32. {
  33.     p->~SomeClass ();    // direct destructor call
  34.     palloc->free (p);    // free memory
  35. }
  36.  
  37. It works, but it is too tiresome to write such function for every class.
  38. A better way is to use template function:
  39.  
  40. template <class Type>
  41. void Delete (Type* p, MemoryAllocator* palloc)
  42. {
  43.     p->~Type ();         // direct destructor call - is it allowed here?
  44.     palloc->free (p);    // free memory
  45. }
  46.  
  47. But if I try to write
  48.  
  49.     MemoryAllocator alloc;
  50.     SomeClass* p = new (&alloc) SomeClass;
  51.     Delete (p, &alloc);
  52.  
  53. then compiler writes something like
  54. filename(line#) : error C2300: 'SomeClass' : class does
  55. not have a destructor called '~Type'
  56.  
  57. Is it an error of compiler? Or direct call of destructor
  58. in template is not allowed? I haven't found something
  59. about it in ARM.
  60.  
  61.  
  62. 2) Again, let's overload global 'operator new' with default parameter:
  63.  
  64. void* operator new (size_t size, MemoryAllocator* palloc = NULL)
  65. {
  66.     return palloc ? palloc->alloc (size) : malloc (size);
  67. }
  68.  
  69. If I write now
  70.  
  71.     char* p = new char [100];
  72.  
  73. then compiler (it seems) must invoke operator new (100, NULL). But when I've
  74. inspected assembler listing, I discovered that compiler invokes standard
  75. library version of 'operator new' rather than overloaded one.
  76. To correct it, I have to write
  77.  
  78. void* operator new (size_t size, MemoryAllocator* palloc)
  79. {
  80.     return palloc ? palloc->alloc (size) : malloc (size);
  81. }
  82. void* operator new (size_t size)
  83. {
  84.     return operator new (size, NULL);
  85. }
  86.  
  87. Now it works OK. Is it also a compiler error?
  88.  
  89. Alex Bobkov.
  90. ---
  91. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  92.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  93.   is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
  94.